package cn.rongdao.jrkabao.ui.cashadvance;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.recyclerview.divider.RecyclerViewDivider;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.deposit.MyLoanBean;
import cn.rongdao.jrkabao.bean.main.personal.PersonUserInfoBean;
import cn.rongdao.jrkabao.model.cashadvance.MyLoanModel;
import cn.rongdao.jrkabao.ui.cashadvance.adapter.MyLoanAdapter;
import cn.rongdao.jrkabao.ui.cashadvance.adapter.MyLoanViewHolder;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;

/**
 * Created by jack on 2017/7/27
 */

public class MyLoanActivity extends BaseAutoFinishActivity implements FRBaseRecyclerAdapter.OnItemClickListener<MyLoanViewHolder> {

    @BindView(R.id.aml_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.aml_ptr_framelayout)
    PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;

    private MyLoanAdapter adapter;
    private List<MyLoanBean> mList = new ArrayList<>();

    public static void start(Context context) {
        FRStartActivity.start(context, MyLoanActivity.class);
    }

    @Override
    protected void initView() {
        titleBarUIComponent.initTitle("我的贷款");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        adapter = new MyLoanAdapter(thisActivity(), this);
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(this));
        ptrRecyclerViewUIComponent.setRecyclerViewDivider(new RecyclerViewDivider(this));
        ptrRecyclerViewUIComponent.setAdapter(adapter);
        initListener();
    }

    private void initListener() {
        ptrRecyclerViewUIComponent.delayRefresh();
        ptrRecyclerViewUIComponent.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                requestData();
            }
        });
    }

    private void requestData() {
        MyLoanModel.getInstance().execute(new FRBaseObserver<PersonUserInfoBean>(this) {
            @Override
            protected void onBaseNext(PersonUserInfoBean data) {
                mList.clear();
                setDataList(data);
                runOnUiThread();
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                runOnUiThread();
            }
        });
    }

    private void setDataList(PersonUserInfoBean data) {
        MyLoanBean myLoanBean = new MyLoanBean();
        myLoanBean.setName("申请中");
        myLoanBean.setResId(R.drawable.icon_cash_record);
        myLoanBean.setCount(data.getApply_count());
        myLoanBean.setUrl(data.getApply_redirect_url());
        mList.add(myLoanBean);

        myLoanBean = new MyLoanBean();
        myLoanBean.setName("还款中");
        myLoanBean.setResId(R.drawable.icon_my_loan);
        myLoanBean.setCount(data.getPaying_count());
        myLoanBean.setUrl(data.getPaying_redirect_url());
        mList.add(myLoanBean);

        myLoanBean = new MyLoanBean();
        myLoanBean.setName("已完成");
        myLoanBean.setResId(R.drawable.icon_loan_finish);
        myLoanBean.setCount(data.getFinished_count());
        myLoanBean.setUrl(data.getFinished_redirect_url());
        mList.add(myLoanBean);
        adapter.setDataList(mList);
    }

    private void runOnUiThread() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ptrRecyclerViewUIComponent.refreshComplete();
            }
        });
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_my_loan;
    }

    @Override
    public void onItemClick(MyLoanViewHolder personalViewHolder, int position) {
        if (!FRString.isEmpty(adapter.getDataList().get(position).getUrl())) {
            WebViewActivity.start(thisActivity(), adapter.getDataList().get(position).getUrl());
        }
    }
}